"""
用户组管理序列化器
"""
from django.contrib.auth.models import Group,Permission
from rest_framework import serializers

# class PermissionSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Permission
#         fields = ['id', 'name']

class GroupsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ['id', 'name', 
        'permissions' # ManyToManyField类型， 记录中间表， 可以完成反序列化校验
        ]

    # def create(self, validated_data):
    #     # 经过校验， permissions变成对象了
    #     permissions = validated_data.pop('permissions')
    #     group = Group.objects.create(**validated_data)
    #     group.permissions.set([permissions])    # permissions字段有set方法，用于中间表插入数据
    #     return group
